Laravel / Views / CRUD - Company Master
CRUD Example : Company Master
-
1. View Pages & Navigation
STEP 1. View blades
We need following blades for company master
1) index.blade.php 2) create.blade.php 3) edit.blade.php 4) show.blade.php So let's just create following file and put bellow code.
1. resources/views/company/index.blade.php@extends('layout') @section('content') Companies List All Create New
Name Details Action Company ! Cochin View Edit Delete Company 2 Cochin View Edit Delete
@endsection@extends('layout') @section('content') Please not that 3. resources/views/company/edit.blade.php
1. @csrf
2. method="POST" in the form tag
@endsection@extends('layout') @section('content') Please not that 4. resources/views/company/show.blade.php
1. @method('PUT') is used to implement the PUT route
2. value attribute to show the value in the form@extends('layout') @section('content') Name: Company 1Details: CochinSTEP 2. Controller
We should create new controller as CompanyController to show the above blades
Normally a controller should have 7 methods as bellow methods: 1)index() 2)create() 3)store() 4)show() 5)edit() 6)update() 7)destroy() Let us create above functions and show the blades
app/Http/Controllers/CompanyController.phpnamespace App\Http\Controllers; use App\Models\Product; use Illuminate\Http\Request; class ProductController extends Controller { public function index() { return view('company.index'); } public function create() { return view('company.create'); } public function store(Request $request) { } public function show($id) { return view('company.show'); } public function edit(Product $product) { return view('company.edit'); } public function update(Request $request, $id) { } public function destroy($id) { } } Note that the function name, parameters and return function STEP 3. Define the routes for above functions
routes/web.php
use App\Http\Controllers\CompanyController; Route::get('companies', [CompanyController::class, 'index']); Route::get('companies/create', [CompanyController::class, 'create']); Route::post('companies', [CompanyController::class, 'store']); Route::get('companies/{id}', [CompanyController::class, 'show']); Route::get('companies/{id}/edit', [CompanyController::class, 'edit']); Route::put('companies/{id}', [CompanyController::class, 'update']); Route::delete('companies/{id}', [CompanyController::class, 'destroty']); STEP 4. Call the routes in the blades
1. to show listing page, use the route url('companies') in 'a href' tag
2. to show create form, use the route url('companies/create') in 'a href' tagopen resources/views/company/index.blade.php and add the url
3. to save the form data, use the route url('companies') in the action of form tagopen resources/views/company/create.blade.php
-
2. Business Logic
STEP 5. Table and Model
1. Create table 'companies' with fields id, name and details
2. Create model 'Company.php'
app/Models/Company.phpnamespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Company extends Model { use HasFactory; protected $fillable = [ 'name', 'detail' ]; } STEP 6 . Save data into db
1. Open the controller app/Http/Controllers/CompanyController.php
2. include the Company model in the controller CompanyController.php
3. add the given code inside the function store()
public function store(Request $request) { $company= new Company; $company->name= $request->name; $company->details= $request->details; $company->save(); } STEP 7 . Get all data to show in the listing page
1. Open the controller app/Http/Controllers/CompanyController.php
2. include the Company model in the controller CompanyController.php
3. add the given code inside the function index()
public function index(Request $request) { $data['company']=Company::get(); return view('company.index', $data); } STEP 8 . Get the data to show in the view page
1. Open the controller app/Http/Controllers/CompanyController.php
2. include the Company model in the controller CompanyController.php
3. add the given code inside the function show()
public function show(Request $request, $id) { $data['company']=Company::find($id); return view('company.show', $data); } STEP 9 . Get the data to show in the form page for editing
1. Open the controller app/Http/Controllers/CompanyController.php
2. include the Company model in the controller CompanyController.php
3. add the given code inside the function edit()
public function edit(Request $request, $id) { $data['company']=Company::find($id); return view('company.edit', $data); } STEP 10 . Delete the data
1. Open the controller app/Http/Controllers/CompanyController.php
2. include the Company model in the controller CompanyController.php
3. add the given code inside the function destroy()
public function destroy(Request $request, $id) { $company=Company::find($id)->delete(); } -
3. Data Presentation
11. Show all data in the listing page
1. open resources/views/company/index.blade.php
2. display the data '$company' which we passed in the index()
3. update the route of View button - url('companies/{id}')
4. update the route of Edit button - url('companies/{id}/edit')
5. update the route of Delete button - url('companies/{id}')
Complete code of index.blade.php@extends('layout') @section('content') Companies List All Create New
Name Details Action {{$row[‘name’]}} {{$row[‘details’]}} View Edit 11. Show data in the edit form
1. open the file resources/views/company/edit.blade.php
2. use 'value' attribute for showing the data in the input element
Complete code of resources/views/company/edit.blade.php
@endsection@extends('layout') @section('content') 13. save the edited data in the edit form
3. add url('companies/{id}') to the action of the form tag in edit.blade.php
14. Display the data in the show.blade.php
1. open resources/views/company/show.blade.php
2. dipaly the data